<?php

namespace app\api\command;
use app\common\model\Packages;
use app\common\model\UserCommission;
use app\common\model\UserPackagesConfig;
use fast\Random;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Order extends Command
{
    protected function configure()
    {
        $this->setName('order')->setHelp('订单查询')->setDescription('订单查询');

    }

    protected function execute(Input $input, Output $output)
    {
        $orderModel = new \app\common\model\Order();
        $order =  $orderModel->where('platform_type',1)->where('is_up',1)->where('is_agency',1)->whereIn('order_status','-1,1,2,3,4')->order('id desc')->select();
        if($order){
            $url = "http://agent.haoka.bx10086.cn/v1/haoka_api/orders";//办理套餐
            $r   = '';
            foreach ($order as $k=>$v){
                sleep(2);
                $packageInfo = Packages::where(['id'=>$v['goods_id']])->find();
                    $data['appid']     = config('site.ooen_appid');
                    $data['timestamp'] = time();
                    $data['nonce_str'] = Random::alnum(32);
                    $data['order_id']  = $v['order_id'];
                    $data['phone']     = $v['shipping_phone'];
                    $string            = ascll($data);
                    $stringSignTemp    = $string.'&appsecret='.config('site.open_appsecret');
                    $data['sign']      = mb_strtoupper(md5($stringSignTemp));
                    $res = httpPost($url,$data);
                    $arr = json_decode($res,true);
                    unset($data);
                    if(!empty($arr['rows'][0])){
                        $rows = $arr['rows'][0];
                        $v->order_status  = $rows['order_status'];
                        if(strpos($rows['remark'],'已激活') !== false){
                            $v->order_status = 1;
                        }

                        if(strpos($rows['remark'],'重新提交') !== false){
                            $v->order_status = 0;
                        }
                        if(strpos($rows['remark'],'100') !== false || strpos($rows['remark'],'可结算') ){
                            orderfencheng($v->id,$v->goods_id);
                                $v->order_status = 5;
                                $v->settlement_time = time();
//                                $userData = findAncestors($v->user_id);
//                                if(!$userData){
//                                    $config = new UserPackagesConfig();
//                                    $rebate = $config->where(['user_id'=>$v->user_id,'packages_id'=>$v->goods_id])->value('money');
//                                   if($rebate){
//                                       $packageInfo['rebate']  = $rebate;
//                                   }
//                                    //顶级代理下单
//                                    dingji($v->user_id,$v->id,$v->goods_id,$packageInfo['rebate'],false,true);
//                                }else{
//                                    array_unshift($userData,$v->user_id);
//                                    $money = moneydigui($v->user_id,$v->goods_id);
//                                    if($money){
//                                        digui($v->user_id, $money,  $v->goods_id,  $v->id,0,true);
//                                    }else{
//                                        //顶级
//                                        $config = new UserPackagesConfig();
//                                        $rebate = $config->where(['user_id'=>$v->user_id,'packages_id'=>$v->goods_id])->value('money');
//                                        if($rebate){
//                                            $packageInfo['rebate']  = $rebate;
//                                        }
//                                        dingji($userData[count($userData)-1],$v['id'],$v->goods_id,$packageInfo['rebate'],false,true);
//                                    }
//                                }
                        }
                        if(!empty($v->admin_image)){
                            $v->order_status = 1;
                        }
                        $v->order_info_json = json_encode($arr['rows'],true);
                        $v->order_status_text = $rows['order_status_text'];
                        $v->rebate            = $rows['rebate'];
                        $v->channel           = $rows['channel'];
                        $v->remark            = $rows['remark'];
                        $v->package_name      = $packageInfo['name'];
                        if(!empty($rows['phone']) && !$v->phone){
                            $v->phone         = $rows['phone'];
                        }
                        if(!empty($rows['shipping_no'])){
                            $v->shipping_no   = $rows['shipping_no'];
                        }
                        if(!empty($rows['shipping_company_name'])){
                            $v->shipping_company_name = $rows['shipping_company_name'];
                        }
                        $v->is_recharged = $rows['is_recharged'];
                        $r = $v->save();
                    }
                    if($r){
                        echo "操作成功";
                    }else{
                        echo "更新失败";
                    }
//                }

            }
        }

    }
}
